/*
 * @FileName: [PwtzUtils.java] 
 * @Package com.bci.pwtz.common.util 
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com.
 * All rights reserved.
 * 
 * This software is the confidential and proprietary 
 * information of BCINFO Technology Limited Company
 * ("Confidential Information"). You shall not disclose 
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement 
 * you entered into with RKY.
 * 
 * $Rev: 3357 $
 * $LastChangedDate$
 * $LastChangedBy: yupan $
 * 
 * @category ICCARD
 * @version 1.1
 * @author $Author: yupan $
 *
 * Change History:[Formatter: author date description] <br/>
 * 1
 * 2
 * 3
*/


package com.bci.pwtz.common.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.bci.pwtz.common.PwtzConstants;

/**
 * 一句话描述 <br/>
 * <p>Description:<br/>
 *    <p>TODO 详细描述
 *
 * <p>For Examples <br/>
 * <PRE>
 *      TODO 代码使用示例
 * </PRE>
 * <p>
 */
public class PwtzUtils
{
    private static final Logger _logger = Logger.getLogger(PwtzUtils.class);
    
    public static String getRandomString(int length) { //length表示生成字符串的长度
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";   
        Random random = new Random();   
        StringBuffer sb = new StringBuffer();   
        for (int i = 0; i < length; i++) {   
            int number = random.nextInt(base.length());   
            sb.append(base.charAt(number));   
        }   
        return sb.toString();   
    }
    
    public static String getRandomNumber(int length) { //length表示生成字符串的长度
        String base = "0123456789";   
        Random random = new Random();   
        StringBuffer sb = new StringBuffer();   
        for (int i = 0; i < length; i++) {   
            int number = random.nextInt(base.length());   
            sb.append(base.charAt(number));   
        }   
        return sb.toString();   
    }
    
    public static Date getMinuteAfter(Date d, int minute) {  
        Calendar now = Calendar.getInstance();  
        now.setTime(d);  
        now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + minute);  
        return now.getTime();  
    }
    
    public static Date getSecondAfter(Date d, int second) {  
        Calendar now = Calendar.getInstance();  
        now.setTime(d);  
        now.set(Calendar.SECOND, now.get(Calendar.SECOND) + second);  
        return now.getTime();  
    }
    
    public static boolean stringFilter(String str, String regEx) {
        if (StringUtils.isBlank(str))
            return false;
        try {
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(str);
            return m.find();
        } catch (Exception e) {
            return false;
        }
    }
    public static boolean isMobile(String mobile) {
        mobile = mobile.trim();
        try {
            if (mobile != null) {
                Pattern p = Pattern
                        .compile("^1\\d{10}$");
                Matcher m = p.matcher(mobile);
                return m.find();
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    
    public static boolean isPwd(String name) {
        name = name.trim();
        try {
            if (name != null && name.length() >= 6 && name.length()<=20) {
                Pattern p = Pattern
                        .compile("^[a-zA-Z0-9_]{6,12}$");
                Matcher m = p.matcher(name);
                return m.find();
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    
    public static boolean isName(String name) {
        try {
            if (name != null) {
            	name = name.trim();
                Pattern p = Pattern
                        .compile("^[0-9A-Za-z]{6,20}$");
                Matcher m = p.matcher(name);
                return m.find();
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    
    public static String date2String(long date, String stringFormat){
        String str="";
        if(date<=0)
            return str;
        try
        {
            DateFormat dateFormat = new SimpleDateFormat(stringFormat);
            str= dateFormat.format(new Date(date));
        }
        catch (Exception e)
        {
            
        }
        return str;
    }
    
    public static String date2String(Date date){
        String stringFormat="yyyy-MM-dd";
        return date2String(date,stringFormat);
    }
    
    public static String date2String(Date date, String stringFormat){
        String str="";
        if(date==null)
            return str;
        try
        {
            DateFormat dateFormat = new SimpleDateFormat(stringFormat);
            str=dateFormat.format(date);
        }
        catch (Exception e)
        {
        }
        return str;
    }
    
    public static String getCurrentTime(String stringFormat){
        if(null == stringFormat || "".equals(stringFormat.trim())){
            stringFormat = "yyyyMMddHHmmss";
        }
        return date2String(System.currentTimeMillis(), stringFormat);
    }
    
    public static String getCurrentTime(){
        return date2String(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss");
    }
    
    public static int passInt(String str) {
        return passInt(str, 0);
    }
    
    public static int passInt(String str, int temp) {
        int result = temp;
        if (StringUtils.isNotEmpty(str)
                && StringUtils.isNumeric(str)) {
            try {
                result = Integer.parseInt(str);
            } catch (Exception e) {
                _logger.info(str + "不能转换为int类型");
                e.printStackTrace();
            }
        }
        return result;
    }
    
    public static long passLong(String str) {
        return passLong(str, 0l);
    }
    
    public static long passLong(String str, long temp) {
        long result = temp;
        if (StringUtils.isNotEmpty(str)
                && StringUtils.isNumeric(str)) {
            try {
                result = Long.parseLong(str);
            } catch (Exception e) {
                _logger.info(str + "不能转换为Long类型");
            }
        }
        return result;
    }
    
    
    public static double passDouble(String str) {
        return passDouble(str, 0.00d);
    }
    
    public static double passDouble(String str, double temp) {
        double result = temp;
        if (StringUtils.isNotEmpty(str)
                && str.matches(PwtzConstants.DAOUBLE_REG)) {
            try {
                result = Double.parseDouble(str);
            } catch (Exception e) {
                _logger.info(str + "不能转换为Double类型");
            }
        }
        return result;
    }
    public static Object validateNull(Object obj) {
    	if(obj instanceof String){
    		if(obj == null || "null".equals(obj)){
    			return "";
    		}
    	}
        return obj==null?"":obj;
    }

    /**
     * 一句话描述 <br/>
     * <p>Description: TODO(详细描述) 
     * <br/>
     * <p>Author: tonic<br/>
     * <p>Date: 2014-9-19-下午2:56:28<br/>
     * <p>
     * @param args   
     * 
     */
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        System.out.println(isMobile("13540269444"));
    }

    public static String parseCharacterArrayToString(String[] array){
        if(array==null||array.length==0){
            return null;
        }
        StringBuffer buf=new StringBuffer();
        for(String obj:array){
            buf.append(obj);
        }
        return buf.toString();
    }
    
    public static Date parseDate(String str,String format){
    	try {
			return new SimpleDateFormat(format).parse(str);
		} catch (Exception e) {
			 _logger.info(str + "不能转换为【"+format+"】类型的日期");
		}
    	return null;
    }
    
    public static Date parseDate(String str){
    	return parseDate(str,"yyyy-MM-dd");
    }
}
